package com.nl.controller.sms5g.service.impl;

import com.nl.controller.sms5g.bean.bo.SignExcelData;
import com.nl.controller.sms5g.service.MailService;
import com.nl.controller.web.global.GlobalConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.List;

/**
 * MailService实现类
 */
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String content) throws MailException {
        SimpleMailMessage message = new SimpleMailMessage();
        // 邮件发送者
        message.setFrom(from);
        // 邮件接受者
        message.setTo(to);
        // 主题
        message.setSubject(subject);
        // 内容
        message.setText(content);
        mailSender.send(message);

    }

    @Override
    public MimeMessage sendAccessoryMail(Session session, List<SignExcelData> list,String excelFileName) throws javax.mail.MessagingException{

        //拼接附件
        MimeMultipart allFile =new MimeMultipart();

        //消息的固定信息
        MimeMessage mimeMessage = new MimeMessage(session);

        //邮件发送人
        mimeMessage.setFrom(new InternetAddress("17856941469@163.com"));
        //邮件接收人，可以同时发送给很多人，我们这里只发给自己；

        //梅和超--18755585253@139.com
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("17856941469@139.com"));
        mimeMessage.setSubject("签名申请"); //邮件主题

        //文本
        MimeBodyPart body2 = new MimeBodyPart();
        Date nowDate = new Date();
        body2.setContent("以下为欢迎名片平台签名申请材料，详见附件，请各位领导审核，感谢！","text/html;charset=utf-8");


        MimeBodyPart body4 = new MimeBodyPart();
        body4.setDataHandler(new DataHandler(new FileDataSource(GlobalConst.SIGNNATURE_EXCEL_PATH+excelFileName)));
        //附件设置名字
        body4.setFileName("签名申请材料.xls");
        allFile.addBodyPart(body4);

        for(int i=0;i<list.size();i++){
            //附件
            MimeBodyPart body3 = new MimeBodyPart();
            body3.setDataHandler(new DataHandler(new FileDataSource(GlobalConst.SIGNNATURE_FILE_PATH+list.get(i).getAuthorizationId())));
            //附件设置名字
            body3.setFileName("【"+list.get(i).getSignName()+"】"+"集团授权书"+list.get(i).getAuthorizationId().substring(list.get(i).getAuthorizationId().indexOf(".")));
            allFile.addBodyPart(body3);
        }




        /*MimeBodyPart body4 = new MimeBodyPart();
        body4.setDataHandler(new DataHandler(new FileDataSource("src/resources/1.txt")));
        body4.setFileName(""); //附件设置名字*/

        //拼装邮件正文内容
        MimeMultipart multipart1 = new MimeMultipart();
        //multipart1.addBodyPart(body1);
        multipart1.addBodyPart(body2);
        //1.文本和图片内嵌成功！
        multipart1.setSubType("related");

        //new MimeBodyPart().setContent(multipart1); //将拼装好的正文内容设置为主体
        MimeBodyPart contentText =  new MimeBodyPart();
        contentText.setContent(multipart1);
        //附件
        //allFile.addBodyPart(body4);
        //正文
        allFile.addBodyPart(contentText);
        //正文和附件都存在邮件中，所有类型设置为mixed；
        allFile.setSubType("mixed");


        //放到Message消息中
        mimeMessage.setContent(allFile);
        mimeMessage.saveChanges();//保存修改


        return mimeMessage;
    }


}
